package com.msc.sdm.materials.widget;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.msc.sdm.ic.pub.SimMgrConnection;
import com.msc.sdm.ic.pub.SimMgrDbObject;
import com.msc.sdm.ic.pub.SimMgrDbObjectList;
import com.msc.sdm.ic.pub.SimMgrQuery;
import com.msc.sdm.materials.model.SMMaterialsModel;
import com.msc.sdm.materials.utils.SMMaterialsUtil;
import com.msc.sdm.ui.widget.Widget;
import com.msc.sdm.ui.widget.exception.WidgetsRuntimeException;
import com.msc.sdm.ui.widget.toolkit.core.WidgetErrorHandler;
import com.msc.sdm.ui.widget.toolkit.jsf.basic.FacesClientSideJS;
import com.msc.sdm.ui.widget.toolkit.sm.visualdata.MultiObjectVisualData;
import com.msc.sdm.ui.widget.toolkit.sm.visualizer.SMBaseWidget;

public class SMMaterialSODWidget extends SMBaseWidget<MultiObjectVisualData>/* implements SMMaterials */ {
	
	public SMMaterialsModel getModel() {
		return (SMMaterialsModel) this.getValue("model");
	}
	
	public void setModel(SMMaterialsModel sMMaterialsModel) {
		this.setValue("model", (Serializable) sMMaterialsModel);
	}
  
	
	protected void build() {
		MultiObjectVisualData multiObjectVisualData = (MultiObjectVisualData) this.getVisualDataObject();
		if (multiObjectVisualData != null) {
			try {
				SimMgrQuery simMgrQuery = multiObjectVisualData.getInputQuery();
				SimMgrConnection simMgrConnection = (SimMgrConnection) this.getContext().getConnection();
				SimMgrDbObject simMgrDbObject = simMgrQuery.retrieveObjects().get(0);
				
				
				//properties
				SimMgrDbObjectList revProperties = simMgrDbObject.evaluate( "matRevisableProperties" ).getDbObjectList();
				 
				Map< String , List< SimMgrDbObject >  > propertiesMap = new HashMap< String , List< SimMgrDbObject > >(); 
				for( int i = 0  ; i < revProperties.size() ; i++ ) {
					
					SimMgrDbObject revPropertyObj = revProperties.get( i );
					String propertyCategory 	  = revPropertyObj.evaluate( "propertyCategory" ).getString();
					
					List< SimMgrDbObject > revPropertySet = propertiesMap.get( propertyCategory );
					if( revPropertySet == null ) {
						//revPropertySet = new ArrayList< SimMgrDbObject >();
						//propertiesMapl
					}
					
				}
				
				
				
				
				this.setModel(SMMaterialsUtil.createMaterialModelForSOD( simMgrDbObject, simMgrConnection,  null,  false));
				List list = SMMaterialsUtil.orderPropertySetNameListByMaterialDisplay((SimMgrQuery) simMgrQuery,
						(List) this.getModel().getAllPropertySetNameList());
				this.getModel().setAllPropertySetNameList(list);
				
//				this.setMatTables( this.getModel().getMatTables(true) );
//				this.setValue("expandWidth", (Serializable) Boolean.valueOf(true));
//				this.getParentWidget().getParentWidget().getParentWidget().setValue("showBorder",
//						(Serializable) Boolean.valueOf(true));
//				this.createPropertySetAndParameterFilter();
//				this.createNewerRevisionNotePanel(simMgrConnection, simMgrDbObject);
//				SMMaterialsWidget.setFilterParameterVisibility(this);
//				this.displayPropertySetTables();
//				this.updatePropertySetFilterImage(this.getModel().getCurrentlySelectedPropertySet());
				
				FacesClientSideJS facesClientSideJS = new FacesClientSideJS();
				facesClientSideJS.setRequestScopeScript("APP.smBrowserScroll.top = 0; APP.smBrowserScroll.left = 0;");
				this.getChildren().add(facesClientSideJS);
			} catch (WidgetsRuntimeException widgetsRuntimeException) {
				WidgetErrorHandler.handleError((Widget) this, (Throwable) widgetsRuntimeException);
			}
		}
	}
	
}
